﻿package com.wandian.view.ui
{
	import com.wandian.util.GDI;
	
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.Graphics;
	import flash.display.Sprite;
	import flash.events.MouseEvent;
	import flash.system.ApplicationDomain;
	import flash.utils.Dictionary;
	import flash.utils.getDefinitionByName;


	public class Button extends Sprite
	{

		private var _btn_BitMap:BitmapData;
		private var _btn_over_BitMap:BitmapData;
		private var _btn_disable_BitMap:BitmapData;
		private var clickHandler:Function;
		private var enable:Boolean=true;
		private  var cache:Dictionary=new Dictionary(true);
		public function Button(btn_BitMapClass:Class,btn_over_BitMapClass:Class,btn_disable_BitMapClass:Class,boolButton:Boolean=false)
		{
			if(btn_BitMapClass){
				_btn_BitMap=new btn_BitMapClass as BitmapData;
			}
			if(btn_over_BitMapClass){
				_btn_over_BitMap=new btn_over_BitMapClass as BitmapData;
			}
			if(btn_disable_BitMapClass){
				_btn_disable_BitMap=new btn_disable_BitMapClass as BitmapData;
			}
			this.buttonMode=boolButton;
			this.addEventListener(MouseEvent.MOUSE_OVER, this.onOver);
			this.addEventListener(MouseEvent.MOUSE_OUT, this.onUp);
			this.addEventListener(MouseEvent.CLICK, this.onClick);
			this.onUp();
		}


		public function setClickHandler(handler:Function):void
		{
			this.clickHandler=handler;
		}

		private function onOver(e:MouseEvent):void
		{
			if (!(this.enable))
			{
				return;
			}
			;
			var g:Graphics=this.graphics;
			g.clear();		
			if (_btn_over_BitMap == null)
			{
				return;
			}	

			GDI.drawBitmapData(g, 0, 0, _btn_over_BitMap);
		}

		private function onUp(e:MouseEvent=null):void
		{
			if (!(this.enable))
			{
				return;
			}
			;
			var g:Graphics=this.graphics;
			g.clear();			
			if (_btn_over_BitMap == null)
			{
				return;
			}
			;

			GDI.drawBitmapData(g, 0, 0, _btn_BitMap);
		}

		private function onDisable():void
		{
			var g:Graphics=this.graphics;
			g.clear();			
			if (_btn_disable_BitMap == null)
			{
				return;
			}
			;

			GDI.drawBitmapData(g, 0, 0, _btn_disable_BitMap);
		}

		private function onClick(e:MouseEvent):void
		{
			if (!(this.enable))
			{
				return;
			}
			;
			if (this.clickHandler != null)
			{
				this.clickHandler(e);
			}
			;
		}

		public function setEnable(f:Boolean):void
		{
			this.enable=f;
			if (this.enable)
			{
				this.onUp();
			}
			else
			{
				this.onDisable();
			}
			;
		}


	}
} //package casino.controls
